<?php
/**
 * Created by PhpStorm.
 * Clerk: yunpeng
 * Date: 2018/12/11
 * Time: 16:17
 */

namespace app\admin\controller;

use app\admin\Controller;
use think\config;
use think\Db;
use think\Exception;
use think\exception\HttpException;

class Order extends Controller
{
    use \app\admin\traits\controller\Controller;
    protected static $isdelete = false;

    protected function filter(&$map)
    {

        if ($this->request->param('order_status')) {
            $map['a.order_status'] = ["=",$this->request->param('order_status')];
        }
        if ($this->request->param('pay_status')) {
            $map['a.pay_status'] = ["=",$this->request->param('pay_status')];
        }
    }

    public function index(){
        $model = new \app\admin\model\Order();

        // 列表过滤器，生成查询Map对象
        $map = $this->search($model);
        if ($this::$isdelete !== false) {
            $map['a.isdelete'] = $this::$isdelete;
        } else {
            $map['a.isdelete'] = 0;
        }

        // 自定义过滤器
        if (method_exists($this, 'filter')) {
            $this->filter($map);
        }

        $listRows = $this->request->param('numPerPage') ?: Config::get("paginate.list_rows");

        // 排序字段 默认为主键名
        $order = $this->request->param('_order') ?: (empty($sortBy) ? $model->getPk() : $sortBy);
        // 接受 sort参数 0 表示倒序 非0都 表示正序
        $asc = false;
        $sort = null !== $this->request->param('_sort') ?
            ($this->request->param('_sort') == 'asc' ? 'asc' : 'desc') :
            ($asc ? 'asc' : 'desc');


        $list = Db::name('order')
            ->alias('a')
            ->field('a.*,b.name as clerk_name')
            ->join('clerk b','a.clerk_id = b.id','left')
            ->where($map)
            //->where('b.isdelete = 0')
            ->group('a.id')
            //->having('b.order_status = 4')
            ->order($order,$sort)
            ->paginate($listRows, false, ['query' => $this->request->get()]);

        $_sort = ($sort == 'asc') ? 'desc' : 'asc';
        $this->view->assign('sort', $_sort);
        $this->view->assign('list', $list);
        $this->view->assign("count", $list->total());
        $this->view->assign("page", $list->render());
        $this->view->assign('numPerPage', $list->listRows());

        return $this->view->fetch();

    }

    public function detail(){
        $model = new \app\admin\model\Goods();
        $id = $this->request->param('id/d');

        $listRows = $this->request->param('numPerPage') ?: Config::get("paginate.list_rows");

        // 排序字段 默认为主键名
        $order = $this->request->param('_order') ?: (empty($sortBy) ? $model->getPk() : $sortBy);
        // 接受 sort参数 0 表示倒序 非0都 表示正序
        $asc = false;
        $sort = null !== $this->request->param('_sort') ?
            ($this->request->param('_sort') == 'asc' ? 'asc' : 'desc') :
            ($asc ? 'asc' : 'desc');
        $list = Db::name('goods')
            ->field('a.*,b.goods_number,b.goods_price,d.name as clerk_name,d.address,d.phone')
            ->alias('a')
            ->join('order_goods b','a.id = b.goods_id','left')
            ->join('order c','b.order_id = c.id','left')
            ->join('clerk d','c.clerk_id = d.id','left')
            ->where('c.id','=',$id)
            ->group('a.id')
            ->order('a.'.$order,$sort)
            ->paginate($listRows, false, ['query' => $this->request->get()]);
        $_sort = ($sort == 'asc') ? 'desc' : 'asc';
        $this->view->assign('sort', $_sort);
        $this->view->assign('list', $list);
        $this->view->assign("count", $list->total());
        $this->view->assign("page", $list->render());
        $this->view->assign('numPerPage', $list->listRows());

        return $this->view->fetch();
    }


    /**
     * 默认删除操作
     */
    public function delete()
    {

        // 启动事务
        Db::startTrans();
        try {
            $id = $this->request->param('id/d');
            Db::name('clerk')->where('id', $id)->update(['isdelete' => 1]);
            Db::name('order')->where('clerk_id', $id)->update(['isdelete' => 1]);
            // 提交事务
            Db::commit();

        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
        }
        return ajax_return_adv('删除成功', '');
    }

    /**
     * 从回收站恢复
     */
    public function recycle()
    {
        // 启动事务
        Db::startTrans();
        try {
            $id = $this->request->param('id/d');
            Db::name('clerk')->where('id', $id)->where('isdelete',1)->update(['isdelete' => '0']);
            Db::name('order')->where('clerk_id', $id)->where('isdelete',1)->update(['isdelete' => '0']);
            // 提交事务
            Db::commit();
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
        }
        return ajax_return_adv('恢复成功', 'index');
    }




}